/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jfreefall;

/**
 *
 * @author joshi
 */



import java.awt.Font;

import java.text.DecimalFormat;
import java.util.LinkedList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.function.Function2D;
import org.jfree.data.function.NormalDistributionFunction2D;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RefineryUtilities;

/**
 * A demonstration application showing how to create a vertical combined chart.
 *
 */
public class graficaT extends JFrame {

    
     private DecimalFormat df = new DecimalFormat("0.0000");
    /**
     * Constructs a new demonstration application.
     *
     * @param title  the frame title.
     */
    public graficaT(final String title,  double tc, double tt, double c, double n) {

        super(title);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setIconImage(new ImageIcon(getClass().getResource("/img/labs.png")).getImage());
        final JFreeChart chart = createCombinedChart(tc,tt,c,n);
        final ChartPanel panel = new ChartPanel(chart, true, true, true, false, true);
        panel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(panel);

    }

    /**
     * Creates a combined chart.
     *
     * @return the combined chart.
     */
    private JFreeChart createCombinedChart(double tc, double tt, double c, double n) {

      
        
       final XYItemRenderer renderer1 = new StandardXYItemRenderer();
       final NumberAxis rangeAxis1 = new NumberAxis("");
        

        final XYTextAnnotation annotation = new XYTextAnnotation("]" , tt , 0.07);
        annotation.setFont(new Font("SansSerif", Font.PLAIN, 60));
        final XYTextAnnotation annotation1 = new XYTextAnnotation("[" , tt*-1, 0.07);
        annotation1.setFont(new Font("SansSerif", Font.PLAIN, 60));
        final XYTextAnnotation annotation4 = new XYTextAnnotation(""+df.format(tt) , tt , 0.13);
        annotation4.setFont(new Font("SansSerif", Font.PLAIN, 14));
        double aux = tt*-1;
        final XYTextAnnotation annotation5 = new XYTextAnnotation(""+df.format(aux) , aux , 0.13);
        annotation5.setFont(new Font("SansSerif", Font.PLAIN, 14));

        final XYTextAnnotation annotation3 = new XYTextAnnotation(c+"%", 0, 0.25);
        annotation3.setFont(new Font("SansSerif", Font.PLAIN, 14));

        XYTextAnnotation annotation2 = null;
        if (tc>5){
            annotation2 = new XYTextAnnotation("Tc "+tc, 4, 0.05);

        }
        else if (tc<-5){
            annotation2 = new XYTextAnnotation("Tc "+tc, -4, 0.05);

        }
        else if (tc>0){
            annotation2 = new XYTextAnnotation("Tc "+tc, tc, 0.05);
        }

        annotation2.setFont(new Font("SansSerif", Font.PLAIN, 14));

       
        Function2D normal = new NormalDistributionFunction2D(0.0, 1.0);
        XYDataset dataset = DatasetUtilities.sampleFunction2D(normal, -5.0, 5.0, 100, " T Student - "+c+"% - n-2 = "+ n);
        
        XYPlot subplot2 = new XYPlot(dataset, null, rangeAxis1, renderer1);


        
        final CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new NumberAxis(""));
        plot.setGap(10.0);

        subplot2.addAnnotation(annotation);
        subplot2.addAnnotation(annotation1);
        subplot2.addAnnotation(annotation2); //tc
        subplot2.addAnnotation(annotation3);
        subplot2.addAnnotation(annotation4);
        subplot2.addAnnotation(annotation5);

        
        plot.add(subplot2, 1);
        plot.setOrientation(PlotOrientation.VERTICAL);

        return new JFreeChart("",JFreeChart.DEFAULT_TITLE_FONT, plot, true);

    }





    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(final String[] args) {

        final graficaT demo = new graficaT("Intervalo de confianza", -81, 2.01, 0.01, 33);
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }

}